<?php echo $this->validation->error_string; ?>
<?php echo form_open($this->curl . '/edit/' . $this->validation->id, 'enctype="multipart/form-data" name="create_form" id="create_form"')?>
<input type="hidden" name="id_test" id="id_test" value="<?php echo $this->validation->id_tests; ?>" />
<input type="hidden" name="id_question" id="id_question" value="<?php echo $this->validation->id; ?>" />

<table class="nostyle forma" width="100%" border="0" cellpadding="3" cellspacing="0">
<tr>
	<th><?php $this->load->view('_elements/obligatory.php'); ?><?php echo $this->question_model->fields['question']?></th>
	<td><div class="textarea_80"><textarea name="question" id="question"><?php echo $this->validation->question; ?></textarea></div></td>
</tr>
<tr>
	<th><?php $this->load->view('_elements/obligatory');?><?php echo $this->question_model->fields['type']?></th>
	<td><div class="input_300"><?php echo form_dropdown('type', $this->question_model->type, array($this->validation->type), 'id="type" onchange="selectTestType(this)"', TRUE)?></div></td>
</tr>
<tr>
	<th><?php echo $this->question_model->fields['is_sample']?></th>
	<td><?php echo form_checkbox('is_sample', 1, ($this->validation->is_sample == 1) ? TRUE : FALSE, 'class="checkbox"')?></td>
</tr>
<tr>
	<th><?php echo $this->question_model->fields['attachment']?></th>
	<td>
		<?php if (!empty($question['attachment'])): ?>
			<a target="_blank" href="/files/uploads/tests/<?php echo $question['attachment']; ?>"><?php echo $question['attachment']; ?></a><br />
		<?php endif; ?>
		<?php echo form_upload('attachment', null, 'size="30"')?>
	</td>
</tr>
<tr id="tr_new_option">
	<td colspan="2"><input onclick="addNewOption()" name="new_option" type="button" class="" id="new_option" value="<?php echo T_('New option')?>" /></td>
</tr>
<?php foreach ($options as $i => $option): ?>
<tr>
	<td colspan="2">
	<fieldset>
		<input type="hidden" value="<?php echo $option->id; ?>" name="id_option[]" />
		<legend>
			<a href="javascript:void(0)" title="Delete" class="btn_del_option" id="opt_<?php echo $option->id; ?>"><img src="/img/close.png" width="16" height="16" /></a><?php echo T_('Option '); ?>
		</legend>
		<table class="nostyle forma">
		<tbody>
		<tr>
			<th style="width: 230px;"><?php echo T_('Answer'); ?></th>
			<td><div class="input_300"><input type="text" class="input-text" value="<?php echo form_prep($option->name); ?>" name="option[]"></div></td>
		</tr>
		<tr>
			<th>Is correct?</th>
			<?php $checked = ($option->is_correct == 1) ? 'checked="checked"' : ''; ?>
			<td><input type="checkbox" <?php echo $checked; ?> value="<?php echo $option->id; ?>" name="is_correct[]"></td>
		</tr>
		</tbody>
		</table>
	</fieldset>
	</td>
</tr>
<?php endforeach; ?>
<tr>
	<td></td>
	<td>
		<input name="edit" type="submit" class="btn btn_save" id="submit" value="<?php echo T_('Submit')?>" />
		<input name="reset"  type="reset"  class="btn btn_clear" id="reset" value="<?php echo T_('Reset')?>" onclick="$('#create_form input[type=text]').val('');" />
		<input name="delete" type="button" class="btn btn_delete" id="delete" value="<?php echo T_('Delete')?>" onclick="if (confirm('<?php echo T_('Are you sure you want to delete this Question?')?>')) location='/admin/questions/delete/<?php echo $this->validation->id; ?>/<?php echo $this->validation->id_tests?>/';return false;" />		
	</td>
</tr>
</table>

<?php echo form_close()?>

<script type="text/javascript">
var num_options = (<?php echo count($options); ?>+1);

	function selectTestType(obj)
	{
		if ($(obj).val() == 2)
		{
			//$('#value').val('').addClass('disabled').attr('disabled', 'disabled');
		}
		else if ($(obj).val() == 1)
		{
			//$('#value').removeClass('disabled').removeAttr('disabled');
		}
	}

	function addNewOption()
	{
		var type = $('#type').val();

		switch(type)
		{
			case '4':
				if (num_options > 2)
				{
					alert('<?php echo T_("You are not allowed to add more options fot this question type."); ?>');
					return;
				}
				break;
		}
		
		num_options += 1;
		
		$('<tr><td colspan="2"><fieldset>'+
			'<input type="hidden" value="" name="id_option[]" /><legend><a href="javascript:void(0)" title="Delete" class="btn_del_option" id="opt_"><img src="/img/close.png" width="16" height="16" /></a>Option</legend><table class="nostyle forma">'+
			'<tr>'+
				'<th style="width: 230px">Answer</th>'+
				'<td><div class="input_300"><input type="text" name="option[]" value="" class="input-text" /></div></td>'+
			'</tr>'+
			'<tr>'+
				'<th>Is correct?</th>'+
				'<td><input type="checkbox" name="is_correct[]" value="" /></td>'+
			'</tr>'+
		  '</table></fieldset></td></tr>').insertAfter("#tr_new_option");

		$('.btn_del_option').unbind().click(function(){
			deleteOption($(this));
		})
	}

	function deleteOption(obj)
	{
		var opt_id = $(obj).attr('id').substr(4);
		if (opt_id)
		{
			$.post(
				  	'/admin/answers/delete/' + opt_id,
				  	{
				  	},
				  	onDeleteSuccess(obj)
				);
		}
		else
		{
			onDeleteSuccess(obj);
		}
	}

	function onDeleteSuccess(obj)
	{       
		$(obj).parent().parent().parent().parent().remove();
		num_options -= 1;
	}
	
	$(document).ready(function(){
		$('.btn_del_option').unbind().click(function(){
			deleteOption($(this));
		})
	});
</script>